package com.ytjj.qmyx.users.service.impl;

import com.ytjj.common.enums.ScoreSourceEnum;
import com.ytjj.qmyx.users.dao.UserScoreDao;
import com.ytjj.qmyx.users.model.AddUserScoreRequest;
import com.ytjj.qmyx.users.model.entity.UserScore;
import com.ytjj.qmyx.users.model.response.ScoreTaskResponse;
import com.ytjj.qmyx.users.service.UserScoreService;
import com.ytjj.qmyx.users.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 *  *
 **/
@Service
public class UserScoreServiceImpl implements UserScoreService {

    @Resource
    private UserScoreDao userScoreDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addScore(AddUserScoreRequest request) {
        if(request.getUserId() == null) return null;
        UserScore userScore = userScoreDao.findUserScore(request.getUserId());
        if(userScore == null) return null;
        ScoreSourceEnum sourceEnum = request.getSource();
        int index = sourceEnum.getIndex();
        Boolean ruleExist = null;
        if(index == 1) ruleExist = userScoreDao.existPersonalData(request.getUserId());
        if(index == 6) ruleExist = userScoreDao.existSignIn(request.getUserId());
        if(ruleExist!= null && ruleExist) return null;
        if(index > 1 && index < 6) {
            boolean exist = userScoreDao.existByUserIdAndSource(request.getUserId(),request.getSource(),request.getUniqueId());
            if(exist) return null;
            int toDay = userScoreDao.countByToday(request.getUserId(),sourceEnum);
            if(toDay >= 5) return null;
        }
        userScore.addScore(sourceEnum);
        userScore.setUniqueId(request.getUniqueId());
        userScoreDao.updateUserScore(userScore);
        userScoreDao.insertUserScoreRecord(userScore);
        return sourceEnum.getMsg();
    }



    @Override
    public Boolean existPersonalData (Integer userId) {
        return userScoreDao.existPersonalData(userId);
    }

    @Autowired
    private UsersService usersService;
    @Override
    public List<ScoreTaskResponse> getScoreTask(Integer id) {
        List<ScoreTaskResponse> tasks = userScoreDao.findUserScoreTask(id);
        ScoreSourceEnum[] values = ScoreSourceEnum.values();
        List<ScoreTaskResponse> res = new ArrayList<>();
        for (ScoreSourceEnum value : values) {
            ScoreTaskResponse scoreTaskResponse = new ScoreTaskResponse();
            String source = value.toString();
            int index = value.getIndex();
            Integer num = index == 1 || index == 6 ? 1 : 5;
            scoreTaskResponse.setSource(source);
            scoreTaskResponse.setNum(num);
            scoreTaskResponse.setCount(0);
            scoreTaskResponse.setScore(value.getScore());
            if(tasks!= null && index != 1){
                ScoreTaskResponse scoreTaskResponse1 = tasks.stream().filter(item -> item.getSource().equals(source)).findAny().orElse(null);
                if(scoreTaskResponse1 != null)  scoreTaskResponse.setCount(scoreTaskResponse1.getCount());
            }
            if(index == 1) {
                scoreTaskResponse.setCount(userScoreDao.existPersonalData(id) ? 1: 0);
            }
            if(index == 6){
                scoreTaskResponse.setCount(usersService.isSign(id));
            }
            res.add(scoreTaskResponse);
        }
        return res;
    }



}
